﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_ShipTo : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string shipto_code;

        public frm_Edit_ShipTo()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.shipto_code = null;
        }

        ////////////////////////////////////
        /// Load data to form
        ///////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(shipto_code))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_ShipTo] WHERE ShipToCode='" + shipto_code + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.shiptoCode.Text = dataReader["ShipToCode"].ToString();
                        this.shiptoName.Text = dataReader["ShipToName"].ToString();
                        this.shiptoPIC.Text = dataReader["SPIC"].ToString();
                        this.addressText.Text = dataReader["Address"].ToString();
                        this.faxNumber.Text = dataReader["Fax"].ToString();
                        this.phoneNumber.Text = dataReader["Telephone"].ToString();
                        if ((bool)dataReader["ImOrEx"])
                        {
                            this.exportCheck.Checked = true;
                        }
                        this.noteText.Text = dataReader["Note"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                    //MessageBox.Show(ex.Message);
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string shipto_name = null;
            string shipto_pic = null;
            string address = null;
            string telephone = null;
            string fax = null;
            int export = 0;
            string note = null;           

            if (string.IsNullOrEmpty(this.shiptoName.Text))
            {
                error = "Please enter ShipTo Name";
            }
            else
            {
                shipto_name = this.shiptoName.Text;
            }
            if (string.IsNullOrEmpty(error))
            {
                shipto_pic = this.shiptoPIC.Text;
                address = this.addressText.Text;                
                fax = this.faxNumber.Text;
                telephone = this.phoneNumber.Text;
                if (this.exportCheck.Checked)
                {
                    export = 1;
                }
                else
                {
                    export = 0;
                }
                note = this.noteText.Text;
                            
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_ShipTo] SET ShipToName = N'" + shipto_name + "', SPIC = '" + shipto_pic + "',"
                        + " Address=N'" + address + "', [Telephone] = '" + telephone + "', [Fax] = '" + fax + "',"
                        + " [ImOrEx] = " + export + ", [Note] = '" + note + "'"
                        + " WHERE ShipToCode='" + shipto_code + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("ShipTo updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update ShipTo. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
